import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from lib.R import R

app = FastAPI()

# 允许所有的来源访问，您也可以指定允许的来源地址
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 这里使用 * 代表允许所有来源访问，也可以指定具体的地址列表
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有HTTP方法
    allow_headers=["*"],  # 允许所有HTTP请求头
)


@app.get("/")
async def root():
    return {"message": "欢迎使用社团活动发布API接口，请通过小程序调用。"}


@app.get("/news")
async def news():
    return R.ok({
        "newsList": [
            {
                "picture": "https://img.zcool.cn/community/01559f59f32ba9a801216a4bc47077.jpg@2o.jpg",
                "newsTitle": "手工贺卡，同庆节日",
                "newsContent": "同庆节日，我们要为同学们准备一份贺卡，贺卡上有我们社团的名字，我们会在活动结束后发放给同学们。",
                "type": "top"
            },
            {
                "picture": "https://youth.chzu.edu.cn/_upload/article/images/a7/22/f89e83c84fa4b88a1fc062521776/68aa0263-3b0c-4255-9a82-f08e0bf72e39.jpg",
                "newsTitle": "迎新活动，有你有我~",
                "newsContent": "我们社团将要举行迎新活动，届时会有各种精彩的节目和游戏等着大家来参加。让我们一起欢度这个特别的节日吧！",
                "type": "activity"
            },
            {
                "picture": "https://tse4-mm.cn.bing.net/th/id/OIF-C.CrXYbSdCDhOAcZ4CZxLQvQ?rs=1&pid=ImgDetMain",
                "newsTitle": "每月例会，掌握社团最新情况",
                "newsContent": "我们社团每个月都会举行例会，让成员们了解社团的最新动态和计划，同时也会有一些互动环节和分享时间。欢迎所有成员参加！",
                "type": "meeting"
            }
        ]
    })


@app.get("/organizational")
async def organizational():
    return R.ok({
        "organizational": {
            "rateDepartment": "理事会",
            "subordinateDepartment": ['秘书部', '宣传部', '组织部'],
            "groupMembers": [{
                "name": "张山",
                "rateDepartment": "秘书部",
                "position": "秘书部部长",
                "phone": "18074990101",
            }, {
                "name": "李四",
                "rateDepartment": "宣传部",
                "position": "宣传部部长",
                "phone": "18074340001",
            },
                {
                    "name": "王武",
                    "rateDepartment": "组织部",
                    "position": "组织部部长",
                    "phone": "18074921201",
                }, {
                    "name": "张扬",
                    "rateDepartment": "组织部",
                    "position": "组织部部员",
                    "phone": "15124990001",
                }]
        }
    })


@app.post("/login")
async def login(user: dict):
    if user["account"] == "admin" and user["password"] == "admin":
        return R.ok({
            "userInfo": {
                "avatar": "https://img0.baidu.com/it/u=4196418831,3741244744&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                "username": "谭期元",
                "account": "admin",
                "phone": "18074990001",
                "sex": "男"
            }
        })
    return R.fail(msg="用户名或密码错误")


if __name__ == "__main__":
    uvicorn.run(app, host="localhost", port=8880)
